package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
//@CrossOrigin
public class GoodsController {

    //private Result<List<Goods>> result = null;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/findAll")
    public Result<List<Goods>> findAll() {
        List<Goods> list = goodsService.list();
        if (list.size()>0){
            return new Result<>(true, StatusCode.OK, "", list);
        }
        return new Result<>(false, StatusCode.ERROR, "", null);

    }

    @PostMapping("/add")
    public Result<List<Goods>> add(@RequestBody Goods goods) {
        boolean a = goodsService.save(goods);
        if (a) {
            return new Result<List<Goods>>(true, StatusCode.OK, "添加成功", null);
        }
        return new Result<List<Goods>>(false, StatusCode.ERROR, "添加失敗", null);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.lambdaQuery(Goods.class);
        IPage<Goods> goodsPage = new Page<>(page, size);
        goodsService.page(goodsPage, wrapper);
        PageResult pageResult = new PageResult(goodsPage.getTotal(), goodsPage.getRecords());
        return new Result(true, StatusCode.OK, "", pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable("page") Integer page,
                                 @PathVariable("size") Integer size,
                                 @RequestBody Goods searchMap) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.lambdaQuery(Goods.class);
        IPage<Goods> goodsPage = new Page<>(page, size);
        if (searchMap.getSn() != null && searchMap.getSn().trim() != "") {
            wrapper.eq(Goods::getSn, searchMap.getSn());
        }

        if (searchMap.getId() != null) {
            wrapper.eq(Goods::getId, searchMap.getId());
        }

        if (searchMap.getName() != null && searchMap.getName().trim() != "") {
            wrapper.eq(Goods::getName, searchMap.getName());
        }

        if (searchMap.getCaption() != null && searchMap.getCaption().trim() != "") {
            wrapper.eq(Goods::getCaption, searchMap.getCaption());
        }

        if (searchMap.getPrice() != null) {
            wrapper.eq(Goods::getPrice, searchMap.getPrice());
        }

        if (searchMap.getNum() != null) {
            wrapper.eq(Goods::getNum, searchMap.getNum());
        }

        if (searchMap.getImage() != null && searchMap.getImage().trim() != "") {
            wrapper.eq(Goods::getImage, searchMap.getImage());
        }

        if (searchMap.getImages() != null && searchMap.getImages().trim() != "") {
            wrapper.eq(Goods::getImages, searchMap.getImages());
        }

        if (searchMap.getCategory() != null && searchMap.getCategory().trim() != "") {
            wrapper.eq(Goods::getCategory, searchMap.getCategory());
        }

        if (searchMap.getBrand() != null && searchMap.getBrand().trim() != "") {
            wrapper.eq(Goods::getBrand, searchMap.getBrand());
        }

        if (searchMap.getSpec() != null && searchMap.getSpec().trim() != "") {
            wrapper.eq(Goods::getSpec, searchMap.getSpec());
        }

        if (searchMap.getSale_num() != null) {
            wrapper.eq(Goods::getSale_num, searchMap.getSale_num());
        }

        if (searchMap.getComment_num() != null) {
            wrapper.eq(Goods::getComment_num, searchMap.getComment_num());
        }

        if (searchMap.getIs_marketable() != null && searchMap.getIs_marketable().trim() != "") {
            wrapper.eq(Goods::getIs_marketable, searchMap.getIs_marketable());
        }

        if (searchMap.getStatus() != null && searchMap.getStatus().trim() != "") {
            wrapper.eq(Goods::getStatus, searchMap.getStatus());
        }

        if (searchMap.getCreatetime() != null) {
            wrapper.eq(Goods::getCreatetime, searchMap.getCreatetime());
        }

        if (searchMap.getUpdatetime() != null) {
            wrapper.eq(Goods::getUpdatetime, searchMap.getUpdatetime());
        }

        goodsService.page(goodsPage, wrapper);
        PageResult pageResult = new PageResult(goodsPage.getTotal(), goodsPage.getRecords());
        return new Result(true, StatusCode.OK, "", pageResult);
    }

    //findById
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Goods goods = goodsService.getById(id);
        if (goods != null) {
            return new Result(true, StatusCode.OK, "查詢成功", goods);

        }
        return new Result(false, StatusCode.ERROR, "未查到", goods);
    }

    //update
    @PutMapping("/{id}")
    public Result update(@RequestBody Goods goods,
            @PathVariable("id") Integer id){
        LambdaUpdateWrapper<Goods> wrapper = Wrappers.lambdaUpdate(Goods.class);
        wrapper.eq(Goods::getId,id);
        boolean b = goodsService.update(goods,wrapper);
        if (b){
            return new Result(true, StatusCode.OK, "更新成功", "yes");
        }
        return new Result(false, StatusCode.ERROR, "更新失敗", "no");
    }

    //delete
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean d = goodsService.removeById(id);
        if (d){
            return new Result(true, StatusCode.OK, "刪除成功", "yes");
        }
        return new Result(false, StatusCode.ERROR, "刪除失敗", "no");
    }
}
